package com.hqq.dao;

import com.hqq.domain.WoocCard;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * WoocCardDao
 * Created by heqianqian on 2018/3/25.
 */
@Repository
public interface WoocCardDao {

    String TABLE_NAME = "wooc_card";
    String SELECT_FIELD = "card_id as cardId,card_title as cardTitle,card_content as cardContent," +
            "card_user_id as cardUserId,card_create_time as cardCreateTime,card_type_id as cardTypeId,card_remark_count as cardRemarkCount";
    String INSERT_FIELD = "card_title,card_content,card_user_id,card_create_time,card_type_id,card_remark_count";

    /**
     * 查询帖子列表
     *
     * @param sortType 排序类型
     * @param typeId   资源类型
     * @return
     */
    List<WoocCard> listWoocCard(@Param("sortType") Long sortType, @Param("typeId") Long typeId);

    /**
     * 根据id查询
     *
     * @param cardId
     * @return
     */
    @Select("SELECT " + SELECT_FIELD + " FROM " + TABLE_NAME + " WHERE card_id = #{cardId}")
    WoocCard findWoocCardById(@Param("cardId") Long cardId);

    /**
     * 插入帖子信息
     *
     * @param woocCard
     * @return
     */
    @SelectKey(before = false, statement = "SELECT LAST_INSERT_ID() AS cardId", keyProperty = "cardId", resultType = Long.class)
    @Insert("INSERT INTO " + TABLE_NAME + "(" + INSERT_FIELD + ") VALUES (#{cardTitle},#{cardContent},#{cardUserId},#{cardCreateTime},#{cardTypeId},#{cardRemarkCount})")
    void insertWoocCard(WoocCard woocCard);

    /**
     * 删除帖子
     *
     * @param cardId
     */
    @Delete("DELETE FROM " + TABLE_NAME + " WHERE card_id = #{cardId}")
    void deleteById(@Param("cardId") Long cardId);

    @Update("UPDATE " + TABLE_NAME + " SET card_remark_count = card_remark_count+1")
    void updateRemarkCount();
}
